package com.ley.springboot.commons.configuration;

import com.ley.springboot.commons.utils.BeanMapper;
import com.ley.springboot.commons.configuration.properties.DozerProperties;
import lombok.extern.slf4j.Slf4j;
import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;


/**
 * dozer auto configuration
 *
 * @author 2036185346@qq.com
 **/
@Configuration
@EnableConfigurationProperties(DozerProperties.class)
@ConditionalOnClass(value = {Mapper.class, DozerBeanMapper.class})
@Slf4j
public class DozerAutoConfiguration {

    private final DozerProperties dozerProperties;

    public DozerAutoConfiguration(DozerProperties dozerProperties) {
        this.dozerProperties = dozerProperties;
        log.info("dozerProperties: {}", dozerProperties);
    }


    /**
     * dozer mapper
     * **/
    @Bean
    @ConditionalOnMissingBean
    public DozerBeanMapper dozerMapper() {
        List<String> mappingFiles = Arrays.asList(dozerProperties.getMappingFiles());
        DozerBeanMapper dozerBeanMapper = new DozerBeanMapper();
        dozerBeanMapper.setMappingFiles(mappingFiles);
        return dozerBeanMapper;
    }


    @Bean
    @ConditionalOnMissingBean
    public BeanMapper beanMapper() {
        return new BeanMapper();
    }
}
